package com.hd.controller;

import com.hd.model.Pic;
import com.hd.platform.server.api.bussiness.SimlarImageService;
import com.hd.platform.util.ImageUtil;
import com.hd.platform.util.image.ImgNode;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by qingshengzheng on 17/5/6.
 */
@Controller
public class SimlarImageController extends BaseControl {
    @Resource
    private SimlarImageService simlarImageService;

    @ResponseBody
    @SuppressWarnings({"unchecked", "rawtypes", "static-access"})
    @RequestMapping(value = "/getSimlarImages", method = {RequestMethod.POST})
    public String getSimlarImage(
            @RequestParam(value = "file", required = false) MultipartFile file,
            HttpServletRequest request, HttpServletResponse response) {
        String path = request.getSession().getServletContext().getRealPath("upload");
        String fileName = file.getOriginalFilename();
        System.out.println(path);
        File toFile = new File("/work/temp/b.jpg");

        try {
            file.transferTo(toFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //File targetFile = new File(path, fileName);
        List<Pic> nodes = simlarImageService.getSimlarPics(toFile);
        return response(nodes);
    }

    @ResponseBody
    @SuppressWarnings({"unchecked", "rawtypes", "static-access"})
    @RequestMapping(value = "/getSimlarByUrl", method = {RequestMethod.POST})
    public String getSimlarByUrl(
            @RequestParam String filepath,
            HttpServletRequest request, HttpServletResponse response) {
        BufferedImage image = null;
        try {
            byte[] imgbytes = ImageUtil.getImageFromURL(filepath);
            image = ImageUtil.bytesToImage(imgbytes);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //File targetFile = new File(path, fileName);
        List<Pic> nodes = simlarImageService.getSimlarPics(image);
        return response(nodes);
    }


    @ResponseBody
    @SuppressWarnings({"unchecked", "rawtypes", "static-access"})
    @RequestMapping(value = "/uploadImg", method = {RequestMethod.POST, RequestMethod.GET})
    public String uploadImg(

            HttpServletRequest request, HttpServletResponse response) {
        String path = request.getSession().getServletContext().getRealPath("upload");
        //String fileName = file.getOriginalFilename();
        //System.out.println(path);
        //File targetFile = new File(path, fileName);
        //simlarImageService.uploadFile(targetFile);
        return response(null);
    }


}
